﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.Basic.SalesOutlet
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.Basic.Basic_SalesOutletData DALSales = new DAL.Basic.Basic_SalesOutletData();
        protected void Page_Load(object sender, EventArgs e)
        {
            wuc_toolbar.DeleteEvent += new EventHandler(DeleteUC);
            if (!IsPostBack)
            {
                ddlsalesname.Items.Insert(0, new ListItem("全部", "0"));
                BindGroup();//绑定领取卡片单位
                BindRpt();
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary> 
        /// <param name="pageIndex"></param>
        private void BindRpt()
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" ISDELETE=0");
            if (txt_salesoutlet_code.Text.Trim().Length > 0)
                sbStr.Append(" and salesoutlet_code = '" + txt_salesoutlet_code.Text.Trim().ToUpper() + "' ");//网点编码
            if (txt_salesoutlet_name.Text.Trim().Length > 0)
                sbStr.Append(" and salesoutlet_name like '%" + txt_salesoutlet_name.Text.Trim().ToUpper() + "%' ");//网点名称
            if (this.ddlsalesname.SelectedItem != null && ddlsalesname.SelectedIndex != 0)
                sbStr.Append(" and Salesinfoid='" + ddlsalesname.SelectedItem.Value.ToString() + "'");//领取卡片单位
            //状态
            if (ddlSTATUS.SelectedValue != "-1")
            {
                sbStr.Append(" and STATUS=" + ddlSTATUS.SelectedValue);
            }
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_basic_SalesOutlet", "ID", " ID ", AspNetPager1.PageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt();
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteUC(object sender, EventArgs e)
        {
            string delId = hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                string errInfo = "";
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = DALSales.Delete(int.Parse(ids[i]));
                    if (resStr.Split(',')[0] == "ok")
                        resCount++;
                    else if (resStr.Split(',')[0] == "err")
                    {
                        errInfo += resStr.Split(',')[1] + "\n";
                    }
                }
                BindRpt();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            WebPageIndex = 1;
            BindRpt();
        }

        #region 绑定机构/网点树列表
        /// <summary>
        /// 绑定根节点
        /// </summary>
        void BindGroup()
        {
            DataTable dt = new CardLogistics.DAL.Basic.Basic_SalesInfoData().GetList(" ISDELETE=0 ", "*");//获取所有节点
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (row["PARENTID"].ToString().Trim() == "0")//绑定根节点
                    {
                        this.ddlsalesname.Items.Insert(ddlsalesname.Items.Count, new ListItem(row["SALESNAME"].ToString(), row["Id"].ToString()));
                        this.BindDropChildItem(this.ddlsalesname, dt, row["Id"].ToString(), 1);
                    }
                }
            }
        }
        /// <summary>
        /// 绑定子节点
        /// </summary>
        /// <param name="d"></param>
        /// <param name="dt"></param>
        /// <param name="id"></param>
        /// <param name="length"></param>
        void BindDropChildItem(DropDownList d, DataTable dt, string id, int length)
        {
            DataRow[] rows = dt.Select("PARENTID='" + id + "'", "Id DESC");//取出id子节点进行绑定
            for (int i = 0; i < rows.Length; i++)
            {
                this.ddlsalesname.Items.Insert(ddlsalesname.Items.Count, new ListItem(this.SpaceLength(length) + rows[i]["SALESNAME"].ToString(), rows[i]["Id"].ToString()));
                this.BindDropChildItem(d, dt, rows[i]["Id"].ToString(), length + 1);//空白数目加1
            }
        }
        /// <summary>
        /// 子节点前面的空白数
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        string SpaceLength(int i)
        {
            string space = "┊";
            for (int j = 0; j < i; j++)
            {
                space += "┈┈";//注意这里的空白是智能abc输入法状态下的v11字符；┊┈┈
            }
            return space;
        }
        #endregion
    }
}